package com.cong.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cong.entity.Axios;
import com.cong.service.AxiosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// 允许所有方法、所有头信息、指定来源
@CrossOrigin(
        origins = "http://localhost:5173",
        methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE},
        allowedHeaders = "*"
)
public class AxiosController {

    @Autowired
    private AxiosService service;

    // 关键修改：返回 List<Axios> 集合，而非 String
    @RequestMapping("/selectAll")
    public List<Axios> selectAll(){ // 返回类型从 String 改为 List<Axios>
        QueryWrapper<Axios> wrapper = new QueryWrapper();
        wrapper.orderByAsc("id");
        List<Axios> list = service.list(wrapper);
        return list; // 直接返回集合，Spring 会自动转为 JSON 数组
    }

    @PostMapping("/save")
    public Axios save(@RequestBody Axios axios) {
        service.save(axios); // 保存数据
        return axios; // 返回包含id、name、age的完整实体
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Integer id){
        return service.removeById(id);
    }

    @PutMapping("/update")
    public Boolean update(@RequestBody Axios axios){
        return service.updateById(axios);
    }

    @GetMapping("/getUserById/{id}")
    public Axios getUserById(@PathVariable("id") Integer id){
        Axios axios = service.getById(id);
        return axios;
    }
}